var CustomerInfoDlg = {CustomerInfoData: {}, zTreeInstance: null, validateFields: {}};//初始化患者信息详情对话框
//清除数据
CustomerInfoDlg.clearData = function () {
    this.customerInfoData = {};
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
CustomerInfoDlg.set = function (key, val) {
    this.customerInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
CustomerInfoDlg.get = function (key) {
    return $("#" + key).val();
};
//关闭此对话框
CustomerInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
//隐藏患者信息选择的树
CustomerInfoDlg.hideCustomerSelectTree = function () {
    $("#parentCustomerMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};
//收集数据
CustomerInfoDlg.collectData = function () {
    this.set('id').set('customerName').set('pseudonym').set('customerPhone').set('customerSex').set('customerAge')
        .set('birthday').set('customerStatus').set('xcustomerStatus').set('referee').set('refereeShip').set('idCardNo').set('fixedTelephone')
        .set('qqNumber').set('wechatNumber').set('isHaveChildren').set('isReturnVisit').set('email').set('zipCode').set('identificationNumber')
        .set('numberOfAudits').set('finalAuditor').set('auditorTime').set('consumAbility').set('nonTransactionLevel').set('appointmentPayService')
        .set('transactionLevel').set('characters').set('consumptionHabits').set('otherContact').set('country').set('address')
        .set('province').set('cities').set('region').set('maritalStatus').set('occupation').set('filingTime').set('medicalRecordNumber')
        .set('arrivalTime').set('fieldTc').set('causeOfNoReturnVisit').set('entryClerk').set('recordPeople').set('reservationsId')
        .set('oneChannel').set('twoChannel').set('thrChannel').set('oneMedium').set('twoMedium').set('thrMedium').set('treatmentUnit')
        .set('archiving').set('toVoidremarks').set('remarks').set('actualAccountBalance').set('giveAccountBalance').set('refundMoney')
        .set('scarchiving').set('treatmentUnit').set('phoneSharShip').set('yellowNumber').set('cardNumber').set('guideDoctor');
};
//验证数据是否为空
CustomerInfoDlg.validate = function () {
    $('#customerInfoForm').data("bootstrapValidator").resetForm();
    $('#customerInfoForm').bootstrapValidator('validate');
    return $("#customerInfoForm").data('bootstrapValidator').isValid();
};

CustomerInfoDlg.getReferee = function () {
    var index = layer.open({
        type: 2,
        title: '添加推荐人',
        area: ['50%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customer/to_referee'
    });
    this.layerIndex = index;
};
//提交添加到店客户
CustomerInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if ($('#refereeShip').val() != "" && $('#referee').val() == "") {
        alert("请输入推荐人！");
        return;
    }
    if ($("#customerName").val() == null || $("#customerName").val() == "") {
        alert("请输入姓名！");
        return;
    }
    if ($("#birthday").val() == null || $("#birthday").val() == "") {
        alert("请输入生日！");
        return;
    }
    var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
    if (resbirthday.test($("#birthday").val()) == false) {
        alert("生日格式不正确，请输入正确的格式，例xxxx-xx-xx");
        return;
    }
    if ($("#customerAge").val() == null || $("#customerAge").val() == "") {
        alert("请输入年龄！");
        return;
    }
    if ($("#customerPhone").val() == null || $("#customerPhone").val() == "") {
        alert("请输入手机号！");
        return;
    }
    if ($("#treatmentUnit").val() == null || $("#treatmentUnit").val() == "") {
        alert("请选择科室！");
        return;
    }
    if ($("#fieldTc").val() == null || $("#fieldTc").val() == "") {
        alert("请选择TC！");
        return;
    }
    if ($("#guideDoctor").val() == null || $("#guideDoctor").val() == "") {
        alert("请选择导医！");
        return;
    }
    if ($("#yellowNumber").val() == null || $("#yellowNumber").val() == "") {
        alert("请输入黄页编号！");
        return;
    }
    var currentSeat = parent.parent.$('#currentSeat').html();  //选定界面
    var ajax = new $ax(Feng.ctxPath + "/customer/add", function (data) {
        if (data == 1) {
            Feng.success("添加成功!");
            if (currentSeat == '到店 &gt; 预约视图') {
                window.parent.$('#calendar').fullCalendar('refetchEvents');
                $('#cancel', document).click();
            } else {
                window.parent.Customer.table.refresh();
                window.parent.Customer.search();
                $('#cancel', document).click();
            }
        } else if (data == 0) {
            alert("客户今日已到过院");
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        $('#cancel', document).click();
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};

//提交添加到店客户
CustomerInfoDlg.addNatureCustomerSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if ($('#refereeShip').val() != "" && $('#referee').val() == "") {
        alert("请输入推荐人！");
        return;
    }
    if ($('#customerName').val() == null || $('#customerName').val() == "") {
        alert("请输入姓名！");
        return;
    }
    if ($("#birthday").val() == null || $("#birthday").val() == "") {
        alert("请输入生日！");
        return;
    }
    var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
    if (resbirthday.test($("#birthday").val()) == false) {
        alert("生日格式不正确，请输入正确的格式，例xxxx-xx-xx");
        return;
    }
    if ($("#customerAge").val() == null || $("#customerAge").val() == "") {
        alert("请输入年龄！");
        return;
    }
    if ($("#customerPhone").val() == null || $("#customerPhone").val() == "") {
        alert("请输入手机号！");
        return;
    }
    if ($("#treatmentUnit").val() == null || $("#treatmentUnit").val() == "") {
        alert("请选择科室！");
        return;
    }
    if ($("#fieldTc").val() == null || $("#fieldTc").val() == "") {
        alert("请选择TC！");
        return;
    }
    if ($("#guideDoctor").val() == null || $("#guideDoctor").val() == "") {
        alert("请选择导医！");
        return;
    }
    if ($("#oneMedium").val() == "请选择" || $("#oneMedium").val() == "" || $("#oneMedium").val() == null) {
        alert("请选择一级媒介！");
        return;
    }
    if ($("#yellowNumber").val() == null || $("#yellowNumber").val() == "") {
        alert("请输入黄页编号！");
        return;
    }
    var currentSeat = parent.parent.$('#currentSeat').html();  //选定界面
    var ajax = new $ax(Feng.ctxPath + "/customer/addNatureCustomer", function (data) {
        var ids=data.ids.split(",");
        if (ids[0] == 1) {
            Feng.success("添加成功!");
            if(data.status==1){
                window.parent.register(data);
            }
            if (currentSeat == '到店 &gt; 预约视图') {
                if(data.status == "1"){
                    window.parent.document.getElementById("childrenIframe").contentWindow.$('#calendar').fullCalendar('refetchEvents');
                }else {
                    window.parent.$('#calendar').fullCalendar('refetchEvents');
                }
                $('#cancel', document).click();
            } else if(currentSeat == '到店 &gt; 患者信息'){
                if(data.status == "1"){
                    window.parent.document.getElementById("childrenIframe").contentWindow.Customer.table.refresh();
                }else {
                    window.parent.Customer.table.refresh();
                }
                $('#cancel', document).click();
            } else {
                $('#cancel', document).click();
            }
        } else {
            alert("客户电话已存在,请去进行匹配！");
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        $('#cancel', document).click();
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};
//新诊到店
CustomerInfoDlg.addTemporaryCustomerOK = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if ($('#refereeShip').val() != "" && $('#referee').val() == "") {
        alert("请输入推荐人！");
        return;
    }
    if ($('#customerName').val() == null || $('#customerName').val() == "") {
        alert("请输入姓名！");
        return;
    }
    if ($("#birthday").val() == null || $("#birthday").val() == "") {
        alert("请输入生日！");
        return;
    }
    var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
    if (resbirthday.test($("#birthday").val()) == false) {
        alert("生日格式不正确，请输入正确的格式，例xxxx-xx-xx");
        return;
    }
    if ($("#customerAge").val() == null || $("#customerAge").val() == "") {
        alert("请输入年龄！");
        return;
    }
    if ($("#customerPhone").val() == null || $("#customerPhone").val() == "") {
        alert("请输入手机号！");
        return;
    }
    if ($("#treatmentUnit").val() == null || $("#treatmentUnit").val() == "") {
        alert("请选择科室！");
        return;
    }
    if ($("#fieldTc").val() == null || $("#fieldTc").val() == "") {
        alert("请选择TC！");
        return;
    }
    if ($("#guideDoctor").val() == null || $("#guideDoctor").val() == "") {
        alert("请选择导医！");
        return;
    }
    if ($("#oneMedium").val() == "请选择" || $("#oneMedium").val() == "" || $("#oneMedium").val() == null) {
        alert("请选择一级媒介！")
        return;
    }
    if ($("#yellowNumber").val() == null || $("#yellowNumber").val() == "") {
        alert("请输入黄页编号！");
        return;
    }
    var currentSeat = parent.parent.$('#currentSeat').html();  //选定界面
    var ajax = new $ax(Feng.ctxPath + "/customer/addTemporaryCustomerOK", function (data) {
        Feng.success("添加成功!");
        if (currentSeat == '到店 &gt; 预约视图') {
            window.parent.$('#calendar').fullCalendar('refetchEvents');
            $('#cancel', document).click();
        } else {
            window.parent.Customer.table.refresh();
            $('#cancel', document).click();
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        $('#cancel', document).click();
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};

CustomerInfoDlg.addOnemuchCustomerSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if ($('#refereeShip').val() != "" && $('#referee').val() == "") {
        alert("请输入推荐人！");
        return;
    }
    if ($('#customerName').val() == null || $('#customerName').val() == "") {
        alert("请输入姓名！");
        return;
    }
    if ($("#customerAge").val() == null || $("#customerAge").val() == "") {
        alert("请输入年龄！");
        return;
    }
    if ($("#birthday").val() == null || $("#birthday").val() == "") {
        alert("请输入生日！");
        return;
    }
    var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
    if (resbirthday.test($("#birthday").val()) == false) {
        alert("生日格式不正确，请输入正确的格式，例xxxx-xx-xx");
        return;
    }
    if ($("#customerPhone").val() == null || $("#customerPhone").val() == "") {
        alert("请输入手机号！");
        return;
    }
    if ($("#treatmentUnit").val() == null || $("#treatmentUnit").val() == "") {
        alert("请选择科室！");
        return;
    }
    if ($("#fieldTc").val() == null || $("#fieldTc").val() == "") {
        alert("请选择TC！");
        return;
    }
    if ($("#guideDoctor").val() == null || $("#guideDoctor").val() == "") {
        alert("请选择导医！");
        return;
    }
    if ($("#oneMedium").val() == "请选择" || $("#oneMedium").val() == "" || $("#oneMedium").val() == null) {
        alert("请选择一级媒介！");
        return;
    }
    if ($("#yellowNumber").val() == null || $("#yellowNumber").val() == "") {
        alert("请输入黄页编号！");
        return;
    }
    var currentSeat = parent.parent.$('#currentSeat').html();  //选定界面
    var ajax = new $ax(Feng.ctxPath + "/customer/addOnemuchCustomerSubmit", function (data) {
        var ids=data.ids;
        var a = ids.split(",");
        if (a[0] == 1) {
            Feng.success("添加成功!");
            if (currentSeat == '到店 &gt; 预约视图') {
                window.parent.$('#calendar').fullCalendar('refetchEvents');
                $('#cancel', document).click();
            } else {
                window.parent.Customer.table.refresh();
                $('#cancel', document).click();
            }
            if(data.status==1){
                window.parent.register(data);
            }
        } else {
            alert("客户电话已存在,请去进行匹配！");
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        $('#cancel', document).click();
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};

CustomerInfoDlg.addRecharge = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var actualAccountBalance = $('#actualAccountBalance').val();
    if (actualAccountBalance != "" && actualAccountBalance == "") {
        alert("请输入充值金额！");
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/recharge/addRecharge", function (data) {
        Feng.success("添加充值成功!");
        window.parent.Customer.table.refresh();
        CustomerInfoDlg.close();
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};
//重咨提交
CustomerInfoDlg.reconsultingSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var isReturnVisit = $('#isReturnVisit').val();
    var causeOfNoReturnVisit = $('#causeOfNoReturnVisit').val();
    var refereeShip = $('#refereeShip').val();
    var referee = $('#referee').val();
    if (refereeShip != "" && referee == "") {
        alert("请输入推荐人！");
        return;
    }
    if (isReturnVisit == 2 && causeOfNoReturnVisit == "") {
        alert("请输入不回访原因！");
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/customer/reconsulting", function (data) {
        if (data == 1) {
            Feng.success("成功!");
            window.parent.Customer.table.refresh();
            CustomerInfoDlg.close();
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentCustomerMenu" || $(event.target).parents("#parentCustomerMenu").length > 0)) {
        CustomerInfoDlg.hideCustomerSelectTree();
    }
};

CustomerInfoDlg.Recommender = function () {
    $("#oneMedium option[value='老带新']").attr("selected", "selected");
    $("#oneMedium").change();
    if ($("#Recommender").val() == '员工') {
        $("#twoMedium option[value='员工介绍']").attr("selected", "selected");
    } else if ($("#Recommender").val() == '患者') {
        $("#twoMedium option[value='朋友介绍']").attr("selected", "selected");
    }
    $("#oneMedium").attr('disabled', true).css('background', '#ebebe4');
    $("#twoMedium").attr('disabled', true).css('background', '#ebebe4');
};

$(function () {
    Feng.initValidator("customerInfoForm", CustomerInfoDlg.validateFields);
});